﻿using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.Infrastructure.Interfaces;
using B.S.Meeting.WriteAPI.Applications.Commands.MeetingReservations;
using MediatR;

namespace B.S.Meeting.WriteAPI.Applications.CommandHandlers.MeetingReservations
{
    public class UpdReservationCommandHandler : IRequestHandler<UpdReservationCommand, APIResult<int>>
    {
        IBaseRepository<MeetingReservation> mrRep;
        ILogger<UpdReservationCommandHandler> logger;

        public UpdReservationCommandHandler(IBaseRepository<MeetingReservation> mrRep, ILogger<UpdReservationCommandHandler> logger)
        {
            this.mrRep = mrRep;
            this.logger = logger;
        }

        public async Task<APIResult<int>> Handle(UpdReservationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await mrRep.UpdAsync(request.Data);
                return new APIResult<int>
                {
                    Code = StatusEnumCode.Ok,
                    Message = "修改会议预约成功",
                    Data = list
                };
            }
            catch (Exception ex)
            {
                logger.LogError("修改会议预约出错", ex.Message);
                throw;
            }
        }
    }
}
